package com.szgr.baoyin.adapter;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.activity.StoreActivity;
import com.szgr.baoyin.model.Goods;
import com.szgr.baoyin.utils.MyTool;

import java.util.List;

/**
 * Created by Jimbo on 2015/9/18.
 */
public class StoreListAdapter extends BaseAdapter {
    private Context mContext;
    private List<Goods> data;

    public StoreListAdapter(Context context,List<Goods> data){
        mContext = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @TargetApi(Build.VERSION_CODES.KITKAT)
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_new_store_list_view, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();
        Goods goods = data.get(position);
        holder.tv_title.setText(goods.getGoods_name());
        holder.tv_price.setText("¥" + goods.getGoods_price() + "元");
        holder.tv_count.setText(goods.getGoods_salenum() + "人付款");
        String goods_jingle = goods.getGoods_jingle();
        String replace = goods_jingle.replace("；", System.lineSeparator());
        holder.tv_post.setText(replace);
//        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.img_head,R.drawable.loading,R.drawable.loading);
//        MyApplication.getmInstance().getImageLoader().get(data.get(position).getGoods_image(), listener);
        MyTool.showPicture(mContext,holder.img_head,goods.getImg());
        holder.tv_adv2.setText(goods.getGoods_jingle2());
        holder.tv_adv3.setText(goods.getGoods_jingle3());
        return convertView;
    }

    public void setData(List<Goods> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public List<Goods> getData() {
        return data;
    }

    private class ViewHolder {
        ImageView img_head;
        TextView tv_title;
        ImageView img_bao;
        ImageView img_hongbao;
        ImageView img_dubi;
        TextView tv_place;
        TextView tv_post;
        TextView tv_price; //价格
        TextView tv_count; //多少人付款
        TextView tv_adv2; //多少人付款
        TextView tv_adv3; //多少人付款

        public ViewHolder(View view){
            img_head = (ImageView) view.findViewById(R.id.store_item_icon);
            tv_title = (TextView) view.findViewById(R.id.store_item_title);
            img_bao = (ImageView) view.findViewById(R.id.store_item_bao);
            img_hongbao = (ImageView) view.findViewById(R.id.store_item_hongbao);
            img_dubi = (ImageView) view.findViewById(R.id.store_item_dubi);
            tv_place = (TextView) view.findViewById(R.id.store_item_place);
            tv_post = (TextView) view.findViewById(R.id.store_item_post_price);
            tv_price = (TextView) view.findViewById(R.id.store_item_price);
            tv_count = (TextView) view.findViewById(R.id.store_item_count);
            tv_adv2 = (TextView) view.findViewById(R.id.fuk_android);
            tv_adv3 = (TextView) view.findViewById(R.id.store_item_place);
        }
    }
}
